package com.kawaiior.asoulworld.items.weapon;

import com.kawaiior.asoulworld.IdlFramework;
import com.kawaiior.asoulworld.items.AsWeaponBase;
import com.kawaiior.asoulworld.util.EntityUtil;
import com.kawaiior.asoulworld.util.IDLGeneral;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.SoundEvents;
import net.minecraft.item.EnumAction;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ActionResult;
import net.minecraft.util.DamageSource;
import net.minecraft.util.EnumHand;
import net.minecraft.util.EnumParticleTypes;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;

import java.util.List;

public class ItemKiraIronStick extends AsWeaponBase {
    public ItemKiraIronStick(String name) {
        super(name);
        this.attack_damage = 6f;
        this.max_damage = 2048;
    }

    @Override
    public EnumAction getItemUseAction(ItemStack stack)
    {
        return EnumAction.BLOCK;
    }

    @Override
    public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn) {
        float range = 4f;
        Vec3d basePos = playerIn.getPositionVector();
        List<EntityLivingBase> entities = worldIn.getEntitiesWithinAABB(EntityLivingBase.class, IDLGeneral.ServerAABB(basePos.addVector(-range, -range, -range), basePos.addVector(range, range, range)));
        IdlFramework.Log("ItemKiraIronStick found %d targets", entities.size());

        for (EntityLivingBase target: entities) {
            if (target.isEntityEqual(playerIn)) {
                continue;
            }
            if (worldIn.isRemote) {
                EntityUtil.SpawnParticleAround(target, EnumParticleTypes.EXPLOSION_NORMAL, 3);
            } else {
                target.attackEntityFrom(DamageSource.causePlayerDamage(playerIn), 10f);
                playerIn.getHeldItem(handIn).damageItem(4, playerIn);
            }
        }

        playerIn.getCooldownTracker().setCooldown(this, 60);
        playerIn.playSound(SoundEvents.BLOCK_ANVIL_LAND, 1f, 0.8f);
        return super.onItemRightClick(worldIn, playerIn, handIn);
    }

}
